package com.ando.aes;



import java.io.*;

/**
 * @author Ando
 * @version 1.0
 * @date 2022/6/3
 */
public class EncryptFile {


    public static void encryptFileECB(String srcFilePath, String destFilePath, String key) {

        //字节数组
        byte[] buf = new byte[1024];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
//       StringBuffer cipher = new StringBuffer();
        String plainText = "";
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8");
            OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
//                writer.write(line);
                plainText = plainText + line;
            }
            String cipher = Encrypt.encryptECB(plainText, key);
            System.out.println(cipher);
            writer.write(cipher);
            br.close();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

//        //字节数组
//        byte[] buf = new byte[1024];
//        int readLen = 0;
//        FileInputStream fileInputStream = null;
//        FileOutputStream fileOutputStream = null;
//
//        String plainText = "";
//        try {
//            //创建FileInputStream对象，用于读取文件
//            fileInputStream = new FileInputStream(srcFilePath);
//            fileOutputStream = new FileOutputStream(destFilePath);
//
//            while ((readLen = fileInputStream.read(buf)) != -1) {
//
//                plainText = plainText + new String(buf, 0, readLen);
//
//            }
//            System.out.println(plainText);
//
//            String cipher = Encrypt.encryptECB(plainText, key);
//            System.out.println(cipher);
//            fileOutputStream.write(cipher.getBytes());
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//
//            try {
//                //关闭输入刘和输出流，释放资源
//                if (fileInputStream != null) {
//                    fileInputStream.close();
//                }
//                if (fileOutputStream != null) {
//                    fileOutputStream.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }

    }


    public static void encryptFileCBC(String srcFilePath, String destFilePath, String key) throws Exception {
//        String srcFilePath = "d:\\hello.text";
//        String destFilePath = "d:\\hello1.txt";
        //字节数组
        byte[] buf = new byte[1024];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
//       StringBuffer cipher = new StringBuffer();
        String plainText = "";
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8");
            OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
//                writer.write(line);
                plainText = plainText + line;
            }
            String cipher = Encrypt.encryptCBC(plainText, key);
            System.out.println(cipher);
            writer.write(cipher);
            br.close();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//        try {
//            //创建FileInputStream对象，用于读取文件
//            fileInputStream = new FileInputStream(srcFilePath);
//            fileOutputStream = new FileOutputStream(destFilePath);
//
//            while ((readLen = fileInputStream.read(buf)) != -1){
//
//                plainText =  plainText + new String(buf, 0, readLen);
//
////               fileOutputStream.write(buf, 0, readLen);
//
//            }
//            System.out.println(plainText);
//
//            String cipher = Encrypt.encryptCBC(plainText, key);
//            System.out.println(cipher);
//            fileOutputStream.write(cipher.getBytes());
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//
//            try {
//                //关闭输入刘和输出流，释放资源
//                if (fileInputStream != null){
//                    fileInputStream.close();
//                }
//                if (fileOutputStream != null){
//                    fileOutputStream.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }
//
//    }

    }
}